package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.SheJiShiDaoImpl;
import com.cyt.dao.impl.XingWenFengLeiDaoImpl;
import com.cyt.entity.XingWenFengLei;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;



public class AdminZiXunGLServlet extends BaseServlet {
	/**
	 * 查询所有新闻资讯分类
	 * localhost:8080/shiping/adminZiXunGL?reqName=getZiXun
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     XingWenFengLeiDaoImpl xingDaoi=new XingWenFengLeiDaoImpl();//创建资讯分类的实现类对象
	     List <XingWenFengLei> xingLists= xingDaoi.getXingWenFengLei();//查到的所有资讯分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(xingLists);//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	/**
	 * 查询指定条数产品分类 分页
	 * localhost:8080/shiping/adminZiXunGL?reqName=getZiXunTS
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunTS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     XingWenFengLeiDaoImpl xingDaoi=new XingWenFengLeiDaoImpl();//创建资讯分类的实现类对象
	     int page=Integer.parseInt(request.getParameter("page"));//接收前台传来的参数
		    int size=Integer.parseInt(request.getParameter("size"));
	     List <XingWenFengLei> xingLists= xingDaoi.getXingWenFengLeiPage(page, size);//查到的所有资讯分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(xingLists);//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	/**
	 * localhost:8080/shiping/adminZiXunGL?reqName=getZiXunTiao
	 * 查询所有表中数据条数
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     XingWenFengLeiDaoImpl xingDaoi=new XingWenFengLeiDaoImpl();//创建资讯分类的实现类对象
	     List <XingWenFengLei> xingLists= xingDaoi.getXingWenFengLei();//查到的所有资讯分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(xingLists.size());//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * 删除
	 * localhost:8080/shiping/adminZiXunGL?reqName=shanChuZiXun
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuZiXun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int ziXunId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
		XingWenFengLeiDaoImpl ziXunDaoi=new XingWenFengLeiDaoImpl();
	    int n =ziXunDaoi.shanZiXunFL(ziXunId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}

	/**
	 *  批量删除
	 * localhost:8080/shiping/adminZiXunGL?reqName=shanChuZiXunS
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuZiXunS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
		XingWenFengLeiDaoImpl ziXunDaoi=new XingWenFengLeiDaoImpl();

		String ids=request.getParameter("ids");
	    int n =ziXunDaoi.shanZiXunFLS(ids);
	    
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 添加资讯分类
	 * localhost:8080/shiping/adminZiXunGL?reqName=AddZiXunFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddZiXunFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
		XingWenFengLeiDaoImpl ziXunDaoi=new XingWenFengLeiDaoImpl();

		String xWFengLeiName=request.getParameter("xWFengLeiName");
		if(xWFengLeiName !=null && !xWFengLeiName.equals("")) {
	    List<XingWenFengLei> XwList=ziXunDaoi.getZiXunFLName(xWFengLeiName);
	    if(XwList.size()==0) {
	    	XingWenFengLei zx= new XingWenFengLei(0, xWFengLeiName);
			
			int n= ziXunDaoi.addZiXunFL(zx);
	        System.out.println(zx);
			
		    if (n>0) {
				out.print(Result.toClient(true, "添加成功", ""));
			} else {
				out.print(Result.toClient(false, "添加失败", ""));

			}
	    }else{
			out.print(Result.toClient(false, "类别名已存在", ""));

	    }
		
	}
		
	     
	    out.flush();
	    out.close();
	}
	
	
	
	/**
	 * 通过id查数据,前台用于数据回显
	 * localhost:8080/shiping/adminZiXunGL?reqName=chaXingWenFengLeiId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaXingWenFengLeiId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
		XingWenFengLeiDaoImpl xinWenDaoi=new XingWenFengLeiDaoImpl();
		int xwflId=Integer.parseInt(request.getParameter("xWFeiLeiId"));
		XingWenFengLei xw= xinWenDaoi.chaXingWenFengLeiId(xwflId);
        
		Gson gson=new Gson();
		String res =gson.toJson(xw);
	    out.print(res);
	    
	    
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 修改新闻类别
	 * localhost:8080/shiping/adminZiXunGL?reqName=xiuGaiXingWenFengLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiXingWenFengLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
		XingWenFengLeiDaoImpl xinWenDaoi=new XingWenFengLeiDaoImpl();
		int xWFeiLeiId=0;
		if(request.getParameter("xWFeiLeiId")!=null && !request.getParameter("xWFeiLeiId").equals("")) {
			xWFeiLeiId=Integer.parseInt(request.getParameter("xWFeiLeiId"));
		}
		String xWFengLeiName=request.getParameter("xWFengLeiName");
		if(xWFengLeiName !=null && !xWFengLeiName.equals("")) {
			List<XingWenFengLei> XwList	=xinWenDaoi.getZiXunFLName(xWFengLeiName);
			if(XwList.size()==0) {
				XingWenFengLei xw= new XingWenFengLei(xWFeiLeiId, xWFengLeiName);
				int n =xinWenDaoi.xiuGaiZiXunFL(xw);//调用删除的方法,获得数据库受影响行数n
			    
			    if (n>0) {//删除成功
					out.print(Result.toClient(true, "修改成功", ""));
				} else {//删除失败
					out.print(Result.toClient(false, "修改失败", ""));

				}
				
			}else{
				out.print(Result.toClient(false, "类别名已存在", ""));
		    
			}
		}
	    out.flush();
	    out.close();
	}
	
}
